const app = getApp()
const {
	userOrder,
	getRelations,
	getOneServiceList
} = require("../../utils/api")
const {
	returnBack
} = require('../../utils/util')

Page({
	data: {
		orderTypeId: null, //订单类型
		hospital: '', //服务名称
		clinicTime: '', //就诊时间
		memberName: '', //就诊人
		tel: '', //联系电话
		price: '', //订单价格
		workerId: '', //康复师id（不一定有）
		workerName: '', //康复师姓名（显示用）
		memberDetail: '', //患者需求

		mode: '', // 最终选择的时间
		dateVisible: false,
		date: new Date('2024-01-01').getTime(), // 支持时间戳传入
		dateText: '',

		// 指定选择区间
		start: '2020-01-01 00:00:00',
		end: '2030-09-09 12:12:12',

		// 就诊人列表
		relationVisible: false, // 是否展示选项卡
		relationText: '', // 选择的就诊人
		relations: [],

		// 一对一服务列表
		oneServiceList: null

	},
	//展示时间框
	showPicker(e) {
		const {
			mode
		} = e.currentTarget.dataset;
		this.setData({
			mode,
			[`${mode}Visible`]: true,
		});
	},
	//隐藏时间框
	hidePicker() {
		const {
			mode
		} = this.data;
		this.setData({
			[`${mode}Visible`]: false,
		});
	},
	//选择时间完成
	onConfirm(e) {
		const {
			value
		} = e.detail;
		const {
			mode
		} = this.data;
		console.log('confirm', value);
		this.setData({
			clinicTime: value,
			[mode]: value,
			[`${mode}Text`]: value,
		});
		console.log(this.data.clinicTime);
		this.hidePicker();
	},
	//选择过程中
	onColumnChange(e) {
		console.log('pick', e.detail.value);
	},

	//显示关系选择框
	onRelationPicker() {
		this.setData({
			relationVisible: true
		});
		console.log(this.data.relations);
	},
	//选定
	onPickerChange(e) {
		const { key } = e.currentTarget.dataset;
		const { label } = e.detail;
		this.setData({
			[`${key}Visible`]: false,
			[`${key}Text`]: label.join(' '),
			memberName: label[0]
		});

		console.log('picker change:', e.detail);
		console.log('选择的就诊人', this.data.memberName);
	},

	//内容输入
	enterHospital(e) {
		this.setData({
			hospital: e.detail.value
		})
		console.log(this.data.hospital);
	},
	enterMemberName(e) {
		this.setData({
			memberName: e.detail.value
		})
		console.log(this.data.memberName);
	},
	enterTel(e) {
		this.setData({
			tel: e.detail.value
		})
		console.log(this.data.tel);
	},
	enterDetail(e) {
		this.setData({
			memberDetail: e.detail.value
		})
		console.log(this.data.memberDetail);
	},

	/** 生成订单 */
	order() {
		if (this.data.hospital == '' || this.data.clinicTime == '' || this.data.memberName == '' || this.data.tel == '') {
			wx.showToast({
				title: '请将信息填写完整！',
				icon: 'none'
			})
		} else if (this.data.memberDetail == '') {
			wx.showToast({
				title: '请填写患者具体需求',
				icon: 'none'
			})
		} else if (app.globalData.currentUser == '') {
			returnBack('/pages/login/login', '请先登陆', 'error')
		} else {
			const {
				orderTypeId,
				tel,
				memberName,
				memberDetail,
				clinicTime,
				hospital,
				workerId,
				price
			} = this.data
			// 如果没有指定康复师，就生成一对一服务订单
			if (this.data.workerId == '' || this.data.workerId == null) {
				console.log("生成订单");
				userOrder(orderTypeId, tel, memberName, memberDetail, clinicTime, hospital, null, price).then((res) => {
					console.log(res.data);
					wx.navigateTo({
						url: '/pages/pay/pay?orderId=' + res.data.id,
					})
				})
			} else {
				// 否则就生成康复治疗订单
				console.log(this.data.workerId);
				userOrder(orderTypeId, tel, memberName, memberDetail, clinicTime, hospital, workerId, price).then((res) => {
					console.log(res.data);
					wx.navigateTo({
						url: '/pages/pay/pay?orderId=' + res.data.id,
					})
				})
			}
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		console.log(options);
		// 1.如果传的是orderTypeId，就是直接从一对一服务入口进来的，只指定订单类型
		if (options.orderTypeId != '' && options.orderTypeId != null) {
			this.setData({
				orderTypeId: options.orderTypeId
			})
		} else {
			// 2.否则就是从康复治疗入口进来的，指定康复师/价格/服务
			this.setData({
				orderTypeId: 1,
				workerId: options.workerId,
				workerName: options.workerName,
				hospital: options.hospital,
				price: options.price
			})
		}
		// 获取当前用户绑定的所有就诊人
		getRelations(app.globalData.currentUser.id).then((res) => {
			console.log(res.data);
			const testList = []
			//1.遍历查出来的就诊人列表，把就诊人id和就诊人姓名放到relations里
			for (let i = 0; i < res.data.length; i++) {
				testList.push({label: res.data[i].name, value: res.data[i].id})
			}
			this.setData({
				relations: testList
			})
		})
		// 获取一对一服务列表
		getOneServiceList().then((res) => {
			// 遍历服务列表，获取服务名称
			for (let i = 0; i < res.data.length; i++) {
				if (res.data[i].id == options.oneServiceId) {
					this.setData({
						hospital: res.data[i].serviceName,
						price: res.data[i].price
					})
				}
			}
			this.setData({
				oneServiceList: res.data
			})
		})
	}
	
});